home *** CD-ROM | disk | FTP | other *** search
/ Delphi Magazine Collection 2001 / Delphi Magazine Collection 20001 (2001).iso / DISKS / Issue30 / rsa10 / RSA10.ZIP / SAMPLE / MAIN.PAS < prev    next >
Encoding:
Pascal/Delphi Source File  |  1997-09-11  |  3.7 KB  |  165 lines

  1. //
  2. // TRSA component demo
  3. //
  4. // Copyright Cygron 1997
  5. //
  6.  
  7. unit main;
  8.  
  9. interface
  10.  
  11. uses
  12.   Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms,
  13.   Dialogs, RSA, StdCtrls, ComCtrls;
  14.  
  15. type
  16.   TForm1 = class(TForm)
  17.     Button1: TButton;
  18.     RSA1: TRSA;
  19.     ButtonEncode: TButton;
  20.     ButtonDecode: TButton;
  21.     Label1: TLabel;
  22.     Memo3: TMemo;
  23.     Edit1: TEdit;
  24.     Edit3: TEdit;
  25.     Label2: TLabel;
  26.     Label3: TLabel;
  27.     LabelProgress: TLabel;
  28.     Button2: TButton;
  29.     Button3: TButton;
  30.     ListBox1: TListBox;
  31.     Label4: TLabel;
  32.     Label5: TLabel;
  33.     UpDown1: TUpDown;
  34.     Edit2: TEdit;
  35.     Label6: TLabel;
  36.     procedure Button1Click(Sender: TObject);
  37.     procedure FormCreate(Sender: TObject);
  38.     procedure FormDestroy(Sender: TObject);
  39.     procedure ButtonEncodeClick(Sender: TObject);
  40.     procedure ButtonDecodeClick(Sender: TObject);
  41.     procedure RSA1Progress(Sender: TObject);
  42.     procedure Button2Click(Sender: TObject);
  43.     procedure Button3Click(Sender: TObject);
  44.   public
  45.     pub,priv: TRSAKey;
  46.     procedure ShowKey;
  47.   end;
  48.  
  49. var
  50.   Form1: TForm1;
  51.  
  52. implementation
  53.  
  54. {$R *.DFM}
  55.  
  56. procedure TForm1.ShowKey;
  57. begin
  58.   ButtonDecode.Enabled := False;
  59.   Edit1.Text := 'Test message';
  60.   ListBox1.Clear;
  61.   Edit3.Text := IntToStr(Priv.m_16Bits);
  62.   Memo3.Clear;
  63.   Memo3.Lines.Add('n=' + pub.m_mod.ToStr);
  64.   Memo3.Lines.Add('e=' + pub.m_a.ToStr);
  65.   Memo3.Lines.Add('d=' + priv.m_a.ToStr);
  66. end;
  67.  
  68. procedure TForm1.FormCreate(Sender: TObject);
  69. begin
  70.   pub := TRSAKey.Create;
  71.   priv := TRSAKey.Create;
  72. end;
  73.  
  74. procedure TForm1.Button1Click(Sender: TObject);
  75. begin
  76.   Randomize;
  77.   Screen.Cursor := crHourglass;
  78.   LabelProgress.Caption := '';
  79.   try
  80.     RSA1.GenerateKey(StrToInt(Edit3.Text),pub,priv);
  81.   finally
  82.     Screen.Cursor := crDefault;
  83.     ShowKey;
  84.     LabelProgress.Caption := '';
  85.     ButtonEncode.Enabled := TRUE;
  86.     Button2.Enabled := TRUE;
  87.     Button3.Enabled := TRUE;
  88.   end;
  89. end;
  90.  
  91. procedure TForm1.FormDestroy(Sender: TObject);
  92. begin
  93.   pub.Free;
  94.   priv.Free;
  95. end;
  96.  
  97. procedure TForm1.ButtonEncodeClick(Sender: TObject);
  98. var
  99.   output: Pointer;
  100.   o: ^TBigArray;
  101.   i,ol: Longint;
  102.   buffer: array[0..1000] of char;
  103.   il: Longint;
  104. begin
  105.   Screen.Cursor := crHourglass;
  106.   LabelProgress.Caption := '';
  107.   strpcopy(buffer,Edit1.Text);
  108.   il := strlen(@buffer);
  109.   RSA1.Encode(pub,@buffer,il,output,ol);
  110.   o := output;
  111.   ListBox1.Clear;
  112.   for i := 1 to ol do ListBox1.Items.Add(IntToStr(o[i]));
  113.   FreeMem(output);
  114.   ButtonDecode.Enabled := True;
  115.   Screen.Cursor := crDefault;
  116. end;
  117.  
  118. procedure TForm1.ButtonDecodeClick(Sender: TObject);
  119. var
  120.   output: Pointer;
  121.   o: ^TBigArray;
  122.   i,ol: Longint;
  123.   buffer: array[0..1000] of byte;
  124.   il: Longint;
  125.   s: String;
  126. begin
  127.   Screen.Cursor := crHourglass;
  128.   LabelProgress.Caption := '';
  129.   for i := 0 to ListBox1.Items.Count-1 do buffer[i] := StrToInt(ListBox1.Items[i]);
  130.   il := ListBox1.Items.Count;
  131.   RSA1.Decode(priv,@buffer,il,output,ol);
  132.   o := output;
  133.   s := '';
  134.   for i := 1 to ol do s := s + char(o[i]);
  135.   Edit2.Text := s;
  136.   FreeMem(output);
  137.   Screen.Cursor := crDefault;
  138. end;
  139.  
  140. procedure TForm1.RSA1Progress(Sender: TObject);
  141. begin
  142.   if Length(LabelProgress.Caption)>40
  143.   then LabelProgress.Caption := 'x'
  144.   else LabelProgress.Caption := LabelProgress.Caption + 'x';
  145.   LabelProgress.Update;
  146. end;
  147.  
  148. procedure TForm1.Button2Click(Sender: TObject);
  149. begin
  150.   pub.Save(ExtractFilePath(Application.ExeName)+'pub.key');
  151.   priv.Save(ExtractFilePath(Application.ExeName)+'priv.key');
  152. end;
  153.  
  154. procedure TForm1.Button3Click(Sender: TObject);
  155. begin
  156.   pub.Load(ExtractFilePath(Application.ExeName)+'pub.key');
  157.   priv.Load(ExtractFilePath(Application.ExeName)+'priv.key');
  158.   Edit3.Text := IntToStr(pub.m_16Bits);
  159.   ShowKey;
  160. end;
  161.  
  162.  
  163.  
  164. end.
  165.